import sys
import os

# 将项目根目录添加到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from abc import ABC, abstractmethod
from datetime import datetime
from types import ModuleType
from typing import List,Optional
from dataclasses import dataclass
from importlib import import_module

from engine.constant import OffsetType, AssetType
from engine.object import BarData, FactorData
from engine.utility import virtual
from engine.assets import Asset


# DB_TZ = ZoneInfo(SETTINGS["database.timezone"])


# def convert_tz(dt: datetime) -> datetime:
#     """
#     Convert timezone of datetime object to DB_TZ.
#     """
#     dt: datetime = dt.astimezone(DB_TZ)
#     return dt.replace(tzinfo=None)


@dataclass
class BarOverview:
    """
    Overview of bar data stored in database.
    """

    symbol: str = ""
    asset_type: AssetType = None
    offset: OffsetType = None
    count: int = 0
    start: datetime = None
    end: datetime = None


@dataclass
class TickOverview:
    """
    Overview of tick data stored in database.
    """

    symbol: str = ""
    asset_type: AssetType = None
    count: int = 0
    start: datetime = None
    end: datetime = None


class BaseDatabase(ABC):
    """
    Abstract database class for connecting to different database.
    """

    @virtual
    def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
        """
        Save bar data into database.
        """
        pass

 

    @virtual
    def load_bar_data(
        self,
        symbol: str,
        asset_type: AssetType,
        offset: OffsetType,
        start: str,
        end: str
    ) -> List[BarData]:
        """
        Load bar data from database.
        """
        pass

 
    @virtual
    def delete_bar_data(
        self,
        symbol: str,
        asset_type: AssetType,
        offset: OffsetType
    ) -> int:
        """
        Delete all bar data with given symbol + asset_type + offset.
        """
        pass

 

    @virtual
    def get_bar_overview(self) -> List[BarOverview]:
        """
        Return bar data avaible in database.
        """
        pass

 
    @virtual
    def construct_asset(self, symbol: str) -> List[Asset]:
        """
        Construct asset from database.
        """
        pass
    @virtual
    def load_factor_data(self, symbol: str,start,end) -> List[FactorData]:
        """
        Load factor data from database.
        """
        pass

    @virtual 
    def get_all_asset_code(self,category:Optional[str]=None) -> str:
        """
        get all asset code
        """
        pass
database: BaseDatabase = None


def get_database() -> BaseDatabase:
    """"""
    # Return database object if already inited
    global database
    if database:
        return database

    # Read database related global setting
    database_name: str = SETTINGS["database.name"]
    module_name: str = f"vnpy_{database_name}"

    # Try to import database module
    try:
        module: ModuleType = import_module(module_name)
    except ModuleNotFoundError:
        print(_("找不到数据库驱动{}，使用默认的SQLite数据库").format(module_name))
        module: ModuleType = import_module("vnpy_sqlite")

    # Create database object from module
    database = module.Database()
    return database